{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "742fb7bc-a0de-4935-afed-eca3b25748b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7da7bb91-a982-4d79-85c7-3ef5d729c2c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import reduce\n",
    "\n",
    "def get_product_of_a_list(arr):\n",
    "    return reduce((lambda x, y: x * y), arr)    \n",
    "\n",
    "def iterate_subarray(arr):\n",
    "    for index,_ in enumerate(arr):\n",
    "        for i,_ in enumerate(range(len(arr)-index)):\n",
    "            sub_array = arr[index:index+i+1]\n",
    "            print(sub_array)\n",
    "            #print(get_product_of_a_list(sub_array))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "034ceaef-a06e-426c-b067-7d6f65072598",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n",
      "[1, 2]\n",
      "[1, 2, 3]\n",
      "[1, 2, 3, 4]\n",
      "[2]\n",
      "[2, 3]\n",
      "[2, 3, 4]\n",
      "[3]\n",
      "[3, 4]\n",
      "[4]\n"
     ]
    }
   ],
   "source": [
    "iterate_subarray(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69c1168e-7fc7-4077-9a5a-585b4b4a15bf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a15e5a8b-59e6-4be8-a18e-3bd204a500cc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "6a46fb27-17e2-4b4f-863a-571d5a8d7998",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = [-1,-1,1,1,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "7fe41843-76b0-4925-8e26-9c0efd09e0f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "for n in nums.copy():\n",
    "    if n == 1:\n",
    "        nums.remove(1)\n",
    "    if n == -1:\n",
    "        count += 1\n",
    "        if count == 2:\n",
    "            nums.remove(-1)\n",
    "            nums.remove(-1)\n",
    "            count = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "f66258f2-7c0c-4f75-b502-766b8a318180",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-1]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a8640cd-b68b-4153-ae2e-fae84203c4f6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8867b10f-c591-4e1d-aad7-44122e842be7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70cc5fef-8676-486b-9c41-41946d12bab1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "dde7892f-c464-4b56-8cfe-7f6c895d1d9b",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/maximum-product-subarray/\n",
    "\n",
    "\n",
    "Runtime: 1708 ms, faster than 5.05% of Python3 online submissions for Maximum Product Subarray.\n",
    "Memory Usage: 14.8 MB, less than 16.07% of Python3 online submissions for Maximum Product Subarray.\n",
    "\n",
    "\n",
    "\n",
    "```python\n",
    "from functools import reduce\n",
    "\n",
    "class Solution:\n",
    "    def maxProduct(self, nums: List[int]) -> int:\n",
    "        #6:50\n",
    "        def decrease_complexity(nums):\n",
    "            count = 0\n",
    "            for n in nums.copy():\n",
    "                if n == 1:\n",
    "                    nums.remove(1)\n",
    "                if n == -1:\n",
    "                    count += 1\n",
    "                    if count == 2:\n",
    "                        nums.remove(-1)\n",
    "                        nums.remove(-1)\n",
    "                        count = 0\n",
    "            return nums\n",
    "        \n",
    "        if len(nums) > 501:\n",
    "            nums = decrease_complexity(nums)\n",
    "        \n",
    "        def get_product_of_a_list(arr):\n",
    "            return reduce((lambda x, y: x * y), arr)    \n",
    "\n",
    "        def iterate_subarray(arr):\n",
    "            max_value = -99999\n",
    "            for index,_ in enumerate(arr):\n",
    "                for i,_ in enumerate(range(len(arr)-index)):\n",
    "                    sub_array = arr[index:index+i+1]\n",
    "                    #sub_array = decrease_complexity(sub_array)\n",
    "                    value = get_product_of_a_list(sub_array)\n",
    "                    if value > max_value:\n",
    "                        max_value = value\n",
    "            return max_value\n",
    "        \n",
    "        return iterate_subarray(nums)\n",
    "        #timeout at 7:12\n",
    "        #try to pass it until 7:25\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd63d498-bfd1-4d40-a633-32cc96e47d6d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
